package com.etime.shuhuiyuan.controllers.lesson;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.common.BaseMessage;
import com.etime.shuhuiyuan.common.ListMessage;
import com.etime.shuhuiyuan.common.PageMessage;
import com.etime.shuhuiyuan.common.PojoMessage;
import com.etime.shuhuiyuan.pojo.Lesson;
import com.etime.shuhuiyuan.services.lesson.interfaces.LessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "lesson接口")
@RestController
@RequestMapping("/lesson")
public class LessonController {
    //注入service层对象
    @Autowired
    private LessonService lessonService;

    /**
     * 插入文本课时
     * @param lesson
     * @return
     */
    @ApiOperation("插入文本课时")
    @ApiImplicitParams({
            @ApiImplicitParam(name="chapterid",value="chapterid"),
            @ApiImplicitParam(name="lessontitle",value = "课时标题"),
            @ApiImplicitParam(name="lessonsummary",value="课时摘要"),
            @ApiImplicitParam(name="lessontype",value = "课时类型"),
            @ApiImplicitParam(name="lessontags",value = "课时标签"),
            @ApiImplicitParam(name="lessoncontent",value = "课时内容"),
            @ApiImplicitParam(name="lessonuserid",value = "课时发布人id"),
            @ApiImplicitParam(name="lessonusertruename",value = "课时发布人姓名"),
    })
    @PostMapping("/insert")
    BaseMessage insertTxtLesson(Lesson lesson){
        BaseMessage msg = new BaseMessage();
        int res = lessonService.insertTxtLesson(lesson);
        msg.setCode(res);
        if (res>0){
            msg.setMsg("插入成功");
        }else {
            msg.setMsg("插入失败");
        }
        return msg;
    }

    /**
     * 更新文本课时
     * @param lesson
     * @return
     */
    @ApiOperation("修改文本课时")
    @ApiImplicitParams({
            @ApiImplicitParam(name="chapterid",value="chapterid"),
            @ApiImplicitParam(name="lessonid",value ="课时id"),
            @ApiImplicitParam(name="lessontitle",value = "课时标题"),
            @ApiImplicitParam(name="lessonsummary",value="课时摘要"),
            @ApiImplicitParam(name="lessontype",value = "课时类型"),
            @ApiImplicitParam(name="lessontags",value = "课时标签"),
            @ApiImplicitParam(name="lessoncontent",value = "课时内容"),
            @ApiImplicitParam(name="lessonuserid",value = "课时发布人id"),
            @ApiImplicitParam(name="lessonusertruename",value = "课时发布人姓名"),
    })
    @PostMapping("/update")
    BaseMessage updateTxtLesson(Lesson lesson){
    BaseMessage msg = new BaseMessage();
    int res = lessonService.updateTxtLesson(lesson);
    if (res>0){
        msg.setCode(1);
        msg.setMsg("修改成功");
    }else {
        msg.setMsg("修改失败");
    }
    return msg;
    }

    /**
     * 分页查询课时
     * @param lesson
     * @param pagesize
     * @param pagenum
     * @return
     */
    @ApiOperation("分页查询课时")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pagenum",value="第几页"),
            @ApiImplicitParam(name="pagesize",value ="每页行数")
    })
    @GetMapping("/page")
    PageMessage<Lesson> selectLessonByPage(Lesson lesson, Long pagesize, Long pagenum){
        Page<Lesson> pagedata = lessonService.selectLessonByPage(lesson, pagesize, pagenum);
        PageMessage<Lesson> msg = new PageMessage<>();
        if (pagedata!=null){
            msg.setCode(1);
            msg.setPagedata(pagedata);
        }else {
            msg.setCode(0);
            msg.setMsg("未查询到数据");
        }
        return msg;
    }

    /**
     * 查询课时列表
     * @param lesson
     * @return
     */
    @ApiOperation("查询课时列表")
    @ApiImplicitParams({

    })
    @GetMapping("/list")
    ListMessage<Lesson> selectLessonList(Lesson lesson){
        List<Lesson> listdata = lessonService.selectLessonList(lesson);
        ListMessage<Lesson> msg = new ListMessage<>();
        if (listdata!=null){
            msg.setDatalist(listdata);
            msg.setCode(1);
        }else {
            msg.setCode(0);
            msg.setMsg("未查询到数据");
        }
        return msg;
    }

    /**
     * 根据id查询一条课时
     * @param lessonid
     * @return
     */
    @ApiOperation("根据id查询一条课时")
    @ApiImplicitParams({
            @ApiImplicitParam(name="lessonid",value="课时id"),
    })
    @GetMapping("/get/{lessonid}")
    PojoMessage<Lesson> selectLessonById(@PathVariable("lessonid")Integer lessonid){
        PojoMessage<Lesson> msg = new PojoMessage<>();
        Lesson lesson = lessonService.selectLessonById(lessonid);
        if (lesson!=null){
            msg.setData(lesson);
            msg.setCode(1);
        }else {
            msg.setMsg("未查询到数据");
        }
        return msg;
    }

    /**
     * 根据chapterid查询课时列表
     * @param chapterid
     * @return
     */
    @ApiOperation("根据chapterid查询课时列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="chapterid",value="章节id"),
    })
    @GetMapping("/listByChapterid/{chapterid}")
    ListMessage<Lesson> selectLessonByChapterid(@PathVariable("chapterid") Integer chapterid){
        ListMessage<Lesson> msg = new ListMessage<>();
        List<Lesson> lessons = lessonService.selectLessonByChaperid(chapterid);
        if (lessons!=null){
            msg.setDatalist(lessons);
            msg.setCode(1);
        }else {
            msg.setCode(0);
            msg.setMsg("未查询到相关数据");
        }
        return msg;
    }

}
